package org.yang.faq.controller;


import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.yang.faq.common.Lay;
import org.yang.faq.common.Message;
import org.yang.faq.model.Faq;
import org.yang.faq.service.FaqService;

import java.util.HashMap;
import java.util.Map;

/**
 * 问题控制器
 */
@Controller
public class FaqController {
    @Autowired
    private FaqService service;

    @GetMapping("/faq")
    public String index() {
        return "/layout.html";
    }

    //    @ApiOperation(value = "添加问题", notes = "让用户添加问题")
//    @ApiImplicitParam(name = "faq", value = "问题实体", required = true, dataType = "Faq")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Message add(Faq faq) {
        return service.insertFaq(faq);
    }

    @RequestMapping(value = "/index/select", method = RequestMethod.GET)
    @ResponseBody
    public Lay select(Lay lay) {
        return service.selectAll(lay);
    }

    @RequestMapping(value = "json", method = RequestMethod.GET)
    public String json() throws Exception {
        throw new Exception("发生错误");
    }

    @ApiOperation(value = "删除问题", notes = "让用户删除问题")
    @ApiImplicitParam(name = "faq", value = "问题实体", required = true, dataType = "Faq")
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Object delete(@RequestBody Faq faq) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", faq.getId());
        boolean b = service.deleteByMap(map);
        return b;
    }
}
